package com.tyk.bookstore.front.member.service.impl;

import com.tyk.bookstore.front.common.core.util.AssertUtil;
import com.tyk.bookstore.front.common.core.util.ObjUtil;
import com.tyk.bookstore.front.common.dal.mapper.dto.SinDto;
import com.tyk.bookstore.front.common.dal.mapper.dto.TripDto;
import com.tyk.bookstore.front.common.security.util.PasswordUtil;
import com.tyk.bookstore.front.member.model.dto.Member4CommentDto;
import com.tyk.bookstore.front.member.model.entity.MemberDetailDo;
import com.tyk.bookstore.front.member.model.entity.MemberDo;
import com.tyk.bookstore.front.member.model.result.MemberResultEnum;
import com.tyk.bookstore.front.member.repository.MemberDetailRepository;
import com.tyk.bookstore.front.member.repository.MemberRepository;
import com.tyk.bookstore.front.member.service.MemberApiService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author tyk
 * @description 会员对内暴露接口
 */
@Service
@RequiredArgsConstructor
public class MemberApiServiceImpl implements MemberApiService {

    private final MemberRepository memberRepository;
    private final MemberDetailRepository memberDetailRepository;


    @Override
    public void deductAmount(Long memberId, String payPwd, BigDecimal deduction) {

        String dpPayPwd = memberRepository.selectPaymentPasswordById(memberId);
        AssertUtil.notBlank(dpPayPwd, MemberResultEnum.PAYMENT_PASSWORD_NOT_SET);

        boolean f = PasswordUtil.checkPassword(payPwd, dpPayPwd);
        AssertUtil.isTrue(f, MemberResultEnum.PAYMENT_PASSWORD_ERROR);

        BigDecimal amount = memberRepository.selectAmountById(memberId);
        AssertUtil.ge(amount, deduction, MemberResultEnum.AMOUNT_INSUFFICIENT);

        memberRepository.incr1Eq(
                MemberDo::getAmount, deduction.negate(),
                MemberDo::getId, memberId,
                MemberDo.class
        );
        memberRepository.deductAmountById(memberId, deduction);

    }

    @Override
    public void increaseAmount(Long memberId, BigDecimal increase) {
        memberRepository.increaseAmountById(memberId, increase);
    }

    @Override
    public String getMemberNameById(Long memberId) {
        SinDto<String> dto = memberDetailRepository.selectSin1Eq(
                MemberDetailDo::getNickname,
                MemberDetailDo::getMemberId, memberId,
                MemberDetailDo.class
        );
        AssertUtil.notNull(dto, MemberResultEnum.MEMBER_NOT_FOUND);
        return dto.getV1();
    }

    @Override
    public List<Member4CommentDto> getMember4CommentDtoListByIdSet(Set<Long> idSet) {
        if (ObjUtil.isEmpty(idSet)) return new ArrayList<>();

        List<TripDto<BigInteger, String, String>> dtoList = memberDetailRepository.selectTripList1In(
                MemberDetailDo::getMemberId, MemberDetailDo::getNickname, MemberDetailDo::getAvatar,
                MemberDetailDo::getMemberId, idSet,
                MemberDetailDo.class
        );

        AssertUtil.equal(dtoList.size(), idSet.size(), MemberResultEnum.MEMBER_NOT_FOUND);
        return dtoList.stream().map(
                it -> new Member4CommentDto(it.getV1().longValue(), it.getV2(), it.getV3())
        ).toList();

    }
}
